class EventEmitter {

  constructor() {
    this.subs = {};
  }

  subscribe(eventName, callback) {
    this.subs[eventName] ??= new Map();

    const key = Symbol();
    this.subs[eventName].set(key, callback);

    return {
      release: () => {
        this.subs[eventName].delete(key);
      }
    }
  }

  emit(eventName, ...args) {
    console.log(this.subs[eventName]);
    for(const callback of this.subs[eventName].values()) {
      try {
        callback.apply(this,...args);
      } catch (e) {
        // Make sure to catch any errors - I was asked this in interview
      }
    }
  }
}

const emitter = new EventEmitter()
function callback1(...args){console.log(this,'111',args)}
function callback2(...args){console.log(this,'222',args)}
const sub1  = emitter.subscribe('event1', callback1)
const sub2 = emitter.subscribe('event2', callback2)
const sub3 = emitter.subscribe('event1', callback2)
sub1.release()
// sub3.release()
console.log(emitter.emit('event1', 1, 2));
